/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-16
 */
package org.jeecg.modules.flow.callback.inbound.other;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.third.entity.EmsOrDmsDeliveryDto;
import org.jeecg.modules.business.third.service.IEmsOrDmsDeliveryOperateService;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 横剪退料(条料)一共需要发给mds两个状态: 1.已接收 2.可配送
 * 1.已接收
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-16
 */
@Component
@Slf4j
public class R14NoticeDeliveryBeforeBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IEmsOrDmsDeliveryOperateService emsOrDmsDeliveryOperateService;
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;

    @Override
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 获取请求参数
        baseFlowDto.translateClass(JSONObject.class);
        String taskId = baseFlowDto.getInfoId();
        //任务修改为执行中
        WmsInReceiptTask task = iWmsInReceiptTaskService.getById(taskId);
        task.setDocumentStatus(DocumentStatusEnum.PROCESS.getCode());
        iWmsInReceiptTaskService.updateById(task);
        log.info("发送退料入库请求");
        //告诉mds任务已接收,相当于通知配送,之后mds开始配送
        EmsOrDmsDeliveryDto deliveryDto = new EmsOrDmsDeliveryDto();
        deliveryDto.setTaskId(taskId);
        deliveryDto.setType(0);
        try {
            boolean warehouse = emsOrDmsDeliveryOperateService.warehouse(deliveryDto);
            if (!warehouse){
                baseFlowDto.setCurrentFailed(true);
                baseFlowDto.setResult(Result.error("调用EMS发送通知已接收请求失败"));
            }
        }catch (Exception e){
            log.error("调用EMS可退空料架异常",e);
            baseFlowDto.setCurrentFailed(true);
            baseFlowDto.setResult(Result.error("调用MDS发送通知已接收请求失败"));
        }
    }
}
